const {
    addSellers,
    isExist,
    getAllSellers,
    getSellers,
    getSellersById,
    changeSellersById,
    delSellersById,
    loginSellers,
    getSellersOrderById,
    getSellersUserById,
    getSellersByIdForClaims
} = require('../dao/sellersDao')


module.exports.loginSellers = async function (params) {
    const data = await loginSellers(params);
    if (data.length > 0) {
        const sellersPassword = params.sellersPassword;
        if (data[0].sellersPassword == sellersPassword) {
            return {
                message: '登录成功',
                status: 1
            }
        }
        else {
            return {
                message: '密码错误',
                status: 0
            }
        }
    }
    else {
        return {
            message: '账号不存在',
            status: 0
        }
    }
}

module.exports.addSellers = async function (params) {
    const data = await isExist({
        sellersAccount: params.sellersAccount
    })
    if (data.length > 0) {
        return ({
            status: "",
            message: "该账号已存在"
        })
    } else {
        const data = await addSellers(params)
        if (data._id) {
            return ({
                message: "添加成功",
                status: 1,
                data
            });
        } else {
            return ({
                message: "添加失败",
                status: 0,
            });
        }
    }
}

module.exports.getAllSellers = async function (params) {
    const data = await getAllSellers(params)
    if (data.length > 0) {
        return {
            message: "获取成功",
            status: 1,
            data
        }
    } else {
        return {
            message: "获取失败",
            status: 0,
        }
    }
}

module.exports.getSellers = async function (params) {
    const data = await getSellers(params)
    try {
        return {
            message: "获取成功",
            status: 1,
            data
        }
    } catch (error) {
        return {
            message: "获取失败",
            status: 0,
        }
    }
}

module.exports.getSellersById = async function (params) {
    const data = await getSellersById(params)
    console.log(data);
    if (data.length > 0) {
        return {
            message: "获取成功",
            status: 1,
            data
        }
    } else {
        return {
            message: "获取失败",
            status: 0,
        }
    }
}

module.exports.changeSellersById = async function (params) {
    const data = await isExist({
        sellersAccount: params.sellersAccount
    })
    if (data.length == 0 || data.length != 0 && data[0]._id == params._id) {
        const data = await changeSellersById(params)
        if (data.nModified > 0) {
            return {
                message: "修改成功",
                status: 1,
                data
            }
        } else {
            return {
                message: "修改失败",
                status: 0,
            }
        }
    } else {
        return {
            message: "修改失败,该账号已存在",
            status: 0,
        }
    }
}

module.exports.delSellersById = async function (params) {
    const data = await delSellersById(params)
    if (data.nModified > 0) {
        return ({
            message: "删除成功",
            status: 1,
        });
    } else {
        return ({
            message: "删除失败",
            status: 0,
        });
    }
}


module.exports.getSellersOrderById = async function (params) {
    const data = await getSellersOrderById(params)
    console.log(data);
    try {
        return {
            message: "获取成功",
            status: 1,
            data
        }
    } catch (error) {
        return {
            message: "获取失败",
            status: 0,
        }
    }
}


module.exports.getSellersUserById = async function (params) {
    const data = await getSellersUserById(params)
    try {
        return {
            message: "获取成功",
            status: 1,
            data
        }
    } catch (error) {
        return {
            message: "获取失败",
            status: 0,
        }
    }
}


module.exports.getSellersByIdForClaims = async function (params) {
    const data = await getSellersByIdForClaims(params)
    try {
        return {
            message: "获取成功",
            status: 1,
            data
        }
    } catch (error) {
        return {
            message: "获取失败",
            status: 0,
        }
    }
}








